package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.UserService;
import com.itheima.web.controller.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    // 注入rabbitTemplate，发送消息
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //用户列表分页
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {

      //  SecurityUtils.getSubject().checkPermission("用户管理");

        //1.调用service查询用户列表
        PageInfo<User> pageInfo =
                userService.findByPage(getLoginCompanyId(), pageNum, pageSize);
        //2.将用户列表保存到request域中
        request.setAttribute("pageInfo", pageInfo);
        //3.跳转到对象的页面
        return "system/user/user-list";
    }

    /**
     * 进入新增用户页面
     */
    //@RequiresPermissions("用户管理")
    @RequestMapping("/toAdd")
    public String toAdd() {
        String companyId = getLoginCompanyId();
        //1.查询所有部门
        List<Dept> deptList = deptService.findAll(companyId);
        //2.存入request域
        request.setAttribute("deptList", deptList);
        return "system/user/user-add";
    }

    /**
     * 新增或更新用户
     */
    @RequestMapping("/edit")
    public String edit(User user) {
        String company = getLoginCompanyId();
        String companyName = getLoginCompanyName();
        user.setCompanyId(company);
        user.setCompanyName(companyName);

        //1.判断是否具有id属性
        if (StringUtils.isEmpty(user.getId())) {
            //2.用户id为空，执行保存
            userService.save(user);
            /* 发送邮件，通过RabbitTemplate实现消息的发送. 邮件发送，交给消息处理系统*/
            Map<String,String> map = new HashMap<>();
            map.put("email",user.getEmail());
            map.put("title","新员工入职提醒邮件");
            map.put("content","欢迎来到SaasExport大家庭，我们是一支有激情的团队！");
            // 发送消息
            rabbitTemplate.convertAndSend("myExchange","msg.email",map);
        } else {
            //3.用户id不为空，执行更新
            userService.update(user);
        }
        return "redirect:/system/user/list";
    }

    /**
     * 进入到修改界面
     */
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(String id) {
        User user = userService.findById(id);
        String companyId = getLoginCompanyId();
        List<Dept> deptList = deptService.findAll(companyId);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("system/user/user-update");
        mv.addObject("user", user);
        mv.addObject("deptList", deptList);
        return mv;
    }

    /**
     * 删除用户
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        userService.delete(id);

        //跳转到修改界面
        return "redirect:/system/user/list";
    }

    /**
     * 用户是角色修改，请求地址："/system/user/roleList.do?id="+id
     * 跳转地址：WEB-INF/pages/system/user/user-role.jsp
     * 请求信息：用户id
     * 响应信息：
     */
    @RequestMapping("/roleList")
    public String roleList(String id) {
        System.out.println(id);
        User user = userService.findById(id);
        request.setAttribute("user", user);
        //传入公司id查询公司的所有角色
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList", roleList);
        List<Role> userRole = roleService.findRoleByUserId(id);
        System.out.println(userRole);
        request.setAttribute("userRoleStr", userRole);
        return "system/user/user-role";
    }

    /**
     * //修改用户所拥有的角色
     * @param roleIds /上传的修改角色列表
     * @return redirect:/system/user/list  //跳转到用户列表页面
     */
    @RequestMapping("/changeRole")
    public String changeRole(@RequestParam("userId") String userId,
                             @RequestParam(value = "roleIds",defaultValue = "") List<String> roleIds) {

        userService.updateRole(userId, roleIds);
        return "redirect:/system/user/list";
    }

}
